package updownLoad.upload;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

@SuppressWarnings(value = { "all" })
public class UpLoadTestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");              //传输的数据是utf-8的
        resp.setContentType("text/html;charset=utf-8"); //告诉浏览器我的resp是用utf-8的传数据的
        
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload sfu = new ServletFileUpload(factory);   
        try {
            List<FileItem> fileItemList =  sfu.parseRequest(req);
            //可以通过getName判断是否是个file,还是普通表单项
            /*普通表单项*/
            FileItem item1 = fileItemList.get(0); 
            String filename1 = item1.getName();               //null
            String fieldName1 = item1.getFieldName();         //username
            String fileType1 = item1.getContentType();        //null
            String content1 = item1.getString();              //shuoGG
            System.out.println(filename1);
            System.out.println(fieldName1);
            System.out.println(fileType1);
            System.out.println(content1);
            
            /*文件表单*/
            FileItem item3 = fileItemList.get(2); 
            String filename3 = item3.getName();              //AT Wifi.txt
            String fieldName3 = item3.getFieldName();        //resumefile
            String fileType3 = item3.getContentType();       //text/plain
            String content3 = item3.getString();             //文件里面的所有正文
            Long fileSize3 = item3.getSize();                //文件大小 304 
            InputStream fileIn3 = item3.getInputStream();    //这就是该文件的输入流--->核心
            BufferedOutputStream filesave3 = new BufferedOutputStream(new FileOutputStream("F:/uploadfile.txt"));
            IOUtils.copy(fileIn3, filesave3);                //保存上传的文件--->核心
            filesave3.close();                               //就算是IOUtils也没有自动flush, 所以记得close
            System.out.println(filename3);
            System.out.println(fieldName3);
            System.out.println(fileType3);
            System.out.println(fileSize3);
            System.out.println(content3);

            
        } catch (FileUploadException e) {
            e.printStackTrace();
        }

    }

}
